//
//  VisitorView.swift
//  WBDemo
//
//  Created by Meng on 16/12/16.
//  Copyright © 2016年 demo. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate : NSObjectProtocol {
    
    func visitorViewDidRegisterButton(visitorView : VisitorView)
    func visitorViewDidLoginButton(visitorView : VisitorView)
    
}

class VisitorView: UIView {

    // 旋转图片
    @IBOutlet weak var rotationImageView: UIImageView!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var infoLabel: UILabel!
    
    @IBOutlet weak var registerButton: UIButton!
    
    @IBOutlet weak var loginButton: UIButton!
    
    
    weak var delegate : VisitorViewDelegate?
    
    
    // func == -
    // class func == +
    class func vistitorView() -> VisitorView {
        return Bundle.main.loadNibNamed("VisitorView", owner: nil, options: nil)?.last as! VisitorView
    }
    
    func setupInfoWithVisitorView(imageName: String?, title: String) {
        
        infoLabel.text = title
        guard let imgNam = imageName else {
            // 设置首页信息
            startAnimation()
            return
        }
        // 设置其他信息
        iconImageView.image = UIImage(named: imgNam)
        rotationImageView.isHidden = true
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        rotationImageView.layer.add(anim, forKey: nil)
    }
    
    @IBAction func clickRegisterButton(_ sender: Any) {
        delegate?.visitorViewDidRegisterButton(visitorView: self)
    }
    
    @IBAction func clickLoginButton(_ sender: Any) {
        delegate?.visitorViewDidLoginButton(visitorView: self)
    }

}
